package JunitTests;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.LinkedList;

import objectClasses.Database;
import objectClasses.Draft;
import objectClasses.Entry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Databasetests {

	@Before
	// Will be performed before each test.
	public void testSetup()
	{
		System.out.println("Setup for Database tests complete.");
	}

	@Test
	public void test() {	
		Database d = Database.getInstance();
		assertEquals("This should represent the same object",d,Database.getInstance());
		Database.addEntry(new Entry("TestUser1","TestMaterials", "TestCategory", new Draft()));
		try{
		Database.addEntry(new Entry(null,null, null, null));
		}
		catch(NullPointerException e){
			System.out.println("NullPointerException caught this is expected no entry can be null");
		}
		try{
			d.addEntry(new Entry(null,null, null, null));
			}
			catch(NullPointerException e){
				System.out.println("Expected, test if database d and " +
						"Database are recieving same data");
			}
		assertSame(Database.getUserEntries("TestUser1"),Database.getUserEntries("TestUser1"));
		assertNotSame(Database.getUserEntries("TestUser1"),Database.getUserEntries("TestUser2"));
		
		Database.DeleteAllUserEntries("TestUser1");
		assertSame(Database.getUserEntries("TestUser1").size(),
				0);
		
		Database.addEntry(new Entry("TestUser1","TestMaterials", "TestCategory1", new Draft()));
		Database.addEntry(new Entry("TestUser1","TestMaterials", "TestCategory2", new Draft()));
		Database.addEntry(new Entry("TestUser1","TestMaterials", "TestCategory3", new Draft()));
		Database.addEntry(new Entry("TestUser1","TestMaterials", "TestCategory4", new Draft()));;
		assertNotSame("Should only be 3 entries allowed",Database.getUserEntries("TestUser1").size(),4);
		
		Database.deleteLastOccurenceEntry("TestUser1");
		for(Entry e: Database.getUserEntries("TestUser1")){
			assertNotSame("Should not have the same category","TestCategory3",e.getCategory());
		}
		Database.addEntry(new Entry("TestUser2","TestMaterials", "TestCategory1", new Draft()));
		Database.addEntry(new Entry("TestUser3","TestMaterials", "TestCategory1", new Draft()));
		Database.addEntry(new Entry("TestUser4","TestMaterials", "TestCategory1", new Draft()));
		Database.addEntry(new Entry("TestUser5","TestMaterials", "TestCategory1", new Draft()));
		LinkedList<Entry> tester = Database.getEntriesBasedOnCat("TestCategory1");
		for(Entry e:tester){
			assertTrue(e.getCategory()=="TestCategory1");
		}
		
		Database.saveFile();
		for(int i=1;i<5;i++){
			Database.DeleteAllUserEntries("TestUser"+i);
		}
		Database.loadFile();
		Database.printAllEntries();
		
	}
	@After
	// Will be performed after each test.
	public void testComplete()
	{
		System.out.println("Database tests complete");
	}

}
